<template>
  <div
    class="waterfall-item"
    @click="$emit('click', post)"
  >
    <div class="card-content">
      <div class="image-container">
        <img
          :src="post.image ? `http://localhost:5000/uploads/${post.image}` : '../assets/default.jpg'"
          class="card-image"
          @load="onImageLoad($event, post._id)"
          @error="handleImageError($event)"
          :alt="post.title"
        />
      </div>
      <div class="post-info">
        <div class="post-title">{{ post.title }}</div>
        <div class="author-info">
          <span class="author">
            <img
              :src="post.authorimg
                    ? `http://localhost:5000/uploads/${post.authorimg}`
                    : 'src/assets/touxiang.png'"
              alt=""
              class="avatar"
            >
            <span>{{ post.author }}</span>
          </span>
          <span class="like-count">
            <img
              src="../assets/zan.png"
              alt=""
              class="like-icon"
            >
            <span>{{ post.likedBy?.length || 0 }}</span>
            <div class="pinglun-icon"></div>
            <span>{{ post.comments?.length || 0 }}</span>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { nextTick } from 'vue';

const props = defineProps({
  post: {
    type: Object,
    required: true
  }
});

defineEmits(['click']);

// 处理图片加载，根据宽高比调整图片高度
function onImageLoad(event, postId) {
  const img = event.target;
  const naturalWidth = img.naturalWidth;
  const naturalHeight = img.naturalHeight;

  // 计算宽高比
  const aspectRatio = naturalWidth / naturalHeight;

  // 根据宽高比决定显示高度
  let displayHeight;
  if (aspectRatio > 1.4) { // 横图
    displayHeight = '120px';
  } else if(aspectRatio > 1.3){// 横图
    displayHeight = '200px';
  } else if (aspectRatio > 1.2) {// 横图
    displayHeight = '240px';
  } else if (aspectRatio < 0.9) { // 竖图
    displayHeight = '280px';
  } else if(aspectRatio < 0.8){
    displayHeight = '300px';
  }else { // 方图
    displayHeight = '200px';
  }

  // 设置图片高度
  img.style.height = displayHeight;

  // 触发瀑布流重排
  nextTick(() => {
    updateCardHeight(img.closest('.waterfall-item'));
  });
}

// 统一更新卡片高度的函数
function updateCardHeight(item) {
  if (!item) return;

  const cardContent = item.querySelector('.card-content');
  if (cardContent) {
    const contentHeight = cardContent.getBoundingClientRect().height;
    const rowGap = 16; // 与CSS中的grid-gap相匹配
    const rowHeight = 1; // grid-auto-rows 设置为1px
    const rowSpan = Math.ceil((contentHeight + rowGap) / (rowHeight + rowGap));
    item.style.gridRowEnd = `span ${rowSpan}`;
  }
}

// 处理图片加载错误
function handleImageError(event) {
  console.error('图片加载失败:', event.target.src);
  // 设置默认图片
  event.target.src = '/src/assets/images/meiyoukecheng.png';
  // 确保更新卡片高度
  updateCardHeight(event.target.closest('.waterfall-item'));
}

// 导出函数以便父组件可以调用
defineExpose({
  updateCardHeight
});
</script>

<style scoped>
/* 瀑布流项目样式 */
.waterfall-item {
  grid-row-end: span auto;
  break-inside: avoid;
  transition: all 0.3s ease;
}

.card-content {
  background-color: #2d2636;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.08);
  min-height: 100px;
  position: relative;
  backdrop-filter: blur(5px);
}

.waterfall-item:hover {
  transform: translateY(-8px);
}

.waterfall-item:hover .card-content {
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4), 0 0 15px rgba(123, 31, 162, 0.3);
  border-color: rgba(123, 31, 162, 0.5);
}

.image-container {
  width: 100%;
  overflow: hidden;
  min-height: 80px;
  background-color: #1a171c;
  position: relative;
}

.image-container::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40px;
  background: linear-gradient(to top, rgba(45, 38, 54, 1), rgba(45, 38, 54, 0));
  z-index: 1;
  pointer-events: none;
}

.card-image {
  width: 100%;
  object-fit: cover;
  transition: transform 0.6s cubic-bezier(0.33, 1, 0.68, 1);
  display: block;
  filter: brightness(1.05);
}

.waterfall-item:hover .card-image {
  transform: scale(1.07);
}

.post-info {
  padding: 15px 18px;
  position: relative;
}

.post-title {
  color: #ffffff;
  font-weight: 600;
  font-size: 16px;
  line-height: 1.4;
  margin-bottom: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* 作者信息样式 */
.author-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #b0bec5;
  margin-top: 5px;
}

.author {
  display: flex;
  align-items: center;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background-color: rgba(22, 18, 26, 0.35);
  padding: 5px 10px;
  border-radius: 20px;
  backdrop-filter: blur(3px);
}

.avatar, .like-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 6px;
}

.pinglun-icon {
  background-image: url(../assets/pinglun.png);
  background-size: 75%;
  background-repeat: no-repeat;
  background-position: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 6px;
}
/* Add compatibility for avatar-sm class that was used in MyClub.vue */
.avatar-sm {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 6px;
}

.like-count {
  display: flex;
  align-items: center;
  background-color: rgba(22, 18, 26, 0.35);
  padding: 5px 10px;
  border-radius: 20px;
  backdrop-filter: blur(3px);
  transition: all 0.3s ease;
}


</style>
